<?php

/**
 * This is the model class for table "questions".
 *
 * The followings are the available columns in table 'questions':
 * @property integer $id
 * @property integer $quiz_id
 * @property string $title
 * @property string $description
 * @property string $status
 * @property integer $points
 * @property string $correct_answer_desc
 * @property string $created
 * @property string $modified
 * @property integer $level
 * @property string $type
 */
class Questions extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Questions the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'questions';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('quiz_id, title, description, status, points, level, type', 'required'),
			array('quiz_id, points, level', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>200),
			array('description', 'length', 'max'=>500),
			array('status', 'length', 'max'=>9),
			array('type', 'length', 'max'=>6),
			array('correct_answer_desc', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, quiz_id, title, description, status, points, correct_answer_desc, created, modified, level, type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'quiz_id' => 'Quiz',
			'title' => 'Question',
			'description' => 'Description',
			'status' => 'Status',
			'points' => 'Points',
			'correct_answer_desc' => 'Correct Answer Desc',
			'created' => 'Created',
			'modified' => 'Modified',
			'level' => 'Level',
			'type' => 'Type',
		);
	}
public function getModel($type, $arrParams) 
	{
		switch($type)
		{		
			case "CHAPTER-ID":
				$chapter=Chapter::model()->findByPk((int)$arrParams['chapterid']);
				return $chapter;
			break;
			case "QUESTION-ID":
				$question=Questions::model()->findByPk((int)$arrParams['questionid']);
				return $question;
			break;
			
			case "QUIZ-ID":
				$quiz=Quiz::model()->findAllBySql('select id from quiz where chapter_id=:x ',array(':x'=>(int)$arrParams['chapterid']));
				//var_dump($quiz);exit;
				return $quiz;
			break;
		}
	}
	public static function getList($type, $arrParams=array())
	{
		switch ($type)
		{case "QUIZ-ID-RAND-LIMIT":
				
				$questions = Questions::model()->findAll(
					'quiz_id=? AND status="published" AND level=? ORDER BY RAND() LIMIT ?',array($arrParams['quizid'],$arrParams['level'],(int)$arrParams['limit'])
					);
				return $questions;
				break;
		} 
	}

public static function getCountByQuizId($quizid,$level) 
	{
		return Questions::model()->count('quiz_id = ? and level=? and status = "published"',array($quizid,$level));	
	}
	public function search($id=null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		if(!empty($id))
		$criteria->compare('quiz_id',$id);
		else
		$criteria->compare('quiz_id',$this->quiz_id);
		
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('points',$this->points);
		$criteria->compare('correct_answer_desc',$this->correct_answer_desc,true);
		if($_SESSION['role_id'] !="1")
		$criteria->compare('user_id',Yii::app()->user->id);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);
		$criteria->compare('level',$this->level);
		$criteria->compare('type',$this->type,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}